package com.jxd.petMgr.dao.impl;

import java.sql.*;

/**
 * @author lzh
 * @version 1.0
 * @Description
 * @date 2021/11/3
 */


public  class BaseDao {
    Connection conn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;

    /**
     * 建立连接
     *
     * @return 连接对象
     */
    public  Connection getConn() {
        //加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/petmgr";
        String uesr = "root";
        String password = "root";
        try {
            conn = DriverManager.getConnection(url, uesr, password);
//            //关闭自动提交
//            conn.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭连接
     *
     * @param conn
     * @param stat
     * @param rs
     */
    public void close(Connection conn, Statement stat, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stat != null) {
                stat.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     *
     * @param conn
     * @param stat
     */
    public void close(Connection conn, Statement stat) {
        try {
            if (stat != null) {
                stat.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
